﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using RolePlayingGameData;
using DC2010.Objects;
using Microsoft.Xna.Framework;
using DC2010;
using RolePlaying.Events;
using Xna.BaseObjects;
using Xna.Base;
using DC2010.Objects.Data;
using DC2012.Render;
using Xna;

namespace RolePlaying.GameScreens.Render
{
    public class DungeonScreenHelper
    {
        public static void Draw(SpriteBatch spriteBatch, AnimatingSprite astex, Vector2 loc, Vector2 pos_origin, float deltaLight, float scale, Color clr)
        {
            astex.Draw(spriteBatch, pos_origin + loc, 0f, SpriteEffects.None, scale, clr);
        }

        public static void Draw(SpriteBatch spriteBatch, AnimatingSprite astex, Vector2 loc, Vector2 pos_origin, Color clr)
        {
            Draw(spriteBatch, astex, loc, pos_origin, 0f, 1f, clr);
        }

        public static void DrawCentered(SpriteBatch spriteBatch, AnimatingSprite sprite, Rectangle outerRect, Vector2 pos_origin, int dispersion, float scale, Color clr)
        {
            int width = sprite.FrameDimensions.X;
            int height = sprite.FrameDimensions.Y;

            int outerWidth = outerRect.Width;
            int outerHeight = outerRect.Height;

            int X = outerRect.X;
            int Y = outerRect.Y;

            if (outerWidth > width)
            {
                //calculate diff
                int diff = outerWidth - width;

                //how much we must shift it
                diff = diff / 2;

                X = outerRect.X + diff + dispersion;
            }

            if (outerHeight > height)
            {
                //calculate diff
                int diff = outerHeight - height;

                //how much we must shift it
                diff = diff / 2;

                Y = outerRect.Y + diff + dispersion;
            }

            sprite.Draw(spriteBatch, new Vector2(X, Y) + pos_origin, 0f, SpriteEffects.None, scale, Color.Gray);
        }	
	
	
        public static void Draw(SpriteBatch spriteBatch, Texture2D tex, Vector2 loc, Vector2 pos_origin, Vector2 scale, Color clr)
        {
            Vector2 v2 = pos_origin + loc;
            spriteBatch.Draw(
                                tex,
                                v2,
                                null,
                                clr,
                                0f,                     // rotation
                                new Vector2(0, 0),
                                scale,
                                SpriteEffects.None,
                                0);
        }

        public static void DrawFlipped(SpriteBatch spriteBatch, Texture2D tex, Vector2 loc, Vector2 pos_origin, Vector2 scale, Color clr)
        {
            spriteBatch.Draw(
                                tex,
                                new Vector2(448*2 + (int)pos_origin.X - (int)(tex.Width*scale.X) - (int)loc.X, (int)(pos_origin + loc).Y), 
                                null, 
                                clr, 
                                0f, 
                                new Vector2(0,0), 
                                scale,
                                SpriteEffects.FlipHorizontally, 
                                0);

        }

        public static void RenderItems(SpriteBatch spriteBatch, TileData t, int A, int B, Vector2 pos_origin, Color clr, bool isFlipped = false, int shift = 0, bool dbl = false)
        {
            if (t.ObjectsOnTile != null)
            {
                //Render wall items if present
                foreach (ObjectOnTile s in t.ObjectsOnTile)
                {
                    if (
                        s.Position == ObjectPosition.North ||
                        s.Position == ObjectPosition.South ||
                        s.Position == ObjectPosition.West ||
                        s.Position == ObjectPosition.East)
                    {
                        //je to wallitem
                        // na tuto pozici nic jineho dat nelze
                        ItemData id = Root.I.ItemSystem.GetData(s.ObjectId);

                        if (isFlipped)
                            A = -(A);

                        int idblkoef=1;
                        if ((dbl)&&(A!=0)) idblkoef = 2;

                        InGameObjectPosition igop = id.Gfx[A, B];

                        if ((igop != null)&&(!string.IsNullOrEmpty(igop.Texture)))
                        {
                            if (!isFlipped)
                            {
                                DungeonScreenHelper.Draw(spriteBatch, TM.GetTex(igop.Texture.Replace(@"\", @"/")), new Vector2(igop.X - shift, igop.Y), pos_origin, new Vector2(((float)igop.Scale / 100) * idblkoef, (float)igop.Scale / 100), clr);
                            }
                            else
                            {
                                DungeonScreenHelper.DrawFlipped(spriteBatch, TM.GetTex(igop.Texture.Replace(@"\", @"/")), new Vector2(igop.X, igop.Y), pos_origin, new Vector2(((float)igop.Scale / 100) * idblkoef, (float)igop.Scale / 100), clr);
                            }
                        }
                     }
                }
            }
        }

        public static int[] wallsizes = { 1000/* dummy */, 324, 216, 140, 92, 60 };
		
		public static void DrawUniversal(int A, int B, SpriteBatch spriteBatch, Vector2 pos_origin)
		{
			int absA = Math.Abs(A);
			int absB = Math.Abs(B);

            Color clrLight = Color.White;// Color.Lerp(Color.White, Color.Black, (float)(absB * 2) / 10 + 0.1f);
			
            Layer l = Root.I.GameSystem.CurrentLayer;
			//TileData t = Root.I.MovementSystem.GetTile(l, 1, -3);
			TileData t = Root.I.MovementSystem.GetTile(l, A, B);
			
			//WallPoint wp = Neocortex.I.WallPoints[-1, -3];
            WallPoint wp = Neocortex.I.WallPoints[-absA, -absB];

            if (t.TileType == TileType.Wall)
            {
				bool IsFlipped = false;
				if (A > 0) IsFlipped = true;
				
				//draw wall
				if (IsFlipped)
				{
                    DungeonScreenHelper.DrawFlipped(spriteBatch, TM.GetTex("WallSets/" + t.WallSet + "/" + Root.I.GameSystem.IsAlt + "Wall_" + Root.GetCode(-absA, B)), wp.Wall, pos_origin, new Vector2(1, 1), clrLight);
				}
				else
				{
                    DungeonScreenHelper.Draw(spriteBatch, TM.GetTex("WallSets/" + t.WallSet + "/" + Root.I.GameSystem.IsAlt + "Wall_" + Root.GetCode(-absA, B)), wp.Wall, pos_origin, new Vector2(1, 1), clrLight);
				}

                int koef = Math.Abs(A);
                if (IsFlipped) koef = -Math.Abs(A);
                
                //draw items
				// side
                DungeonScreenHelper.RenderItems(spriteBatch, t, A, B, pos_origin, clrLight, IsFlipped, 0, absA >= 2);
				
				// front
				DungeonScreenHelper.RenderItems(spriteBatch, t, 0/* FRONT */, B, pos_origin, clrLight, false, koef*wallsizes[absB], /* pro A <= -2 ==> double image width*/ absA >= 2);

				//items on floor ?
				
				// monsters ?
            }		
			
		}
    }
}
